
using System.Collections.Generic;
using UnityEngine;
using MyTools;
using System;
/// <summary>
/// 挂载在蛊上, 用来确定蛊触发效果的时机, 也就是 通过AddComponent(***Influence) 添加Influence 的时机
/// 可供继承使用的成员:
/// public ConditionType conditionType: 触发的种类, 会在Update中分类讨论.
/// public Type influence: 需要添加到蛊上的Influence. 默认为BaseInfluence, 请在继承时重新引用需要的Influence组件.
/// BaseFindTarget findTarget: 引用蛊上的FindTarget 组件, 用来获得需要作用效果的目标(通过findTarget.GetTargets();)
/// List<GameObject> influencedObjects: 已经作用过influence的物体列表
/// 函数: 
/// protected virtual void Awake(): 默认实现为引用findTarget
/// protected virtual void Start(): 默认实现为空
/// protected virtual void Update(): 默认实现为根据conditionType分类讨论采用不同的StartCondition策略
/// protected virtual bool CustomCheck(): 在conditionType为Custom, Update会调用他返回是否作用效果
/// protected virtual void InfluenceFoundGameObjects(): 默认实现为获得findTarget中找到的各个对象进行_StartFunction
/// protected virtual void End(): 默认实现为将自己deactivate并销毁, 会在Update中持续检查findTarget是否有效, 无效的话自己也会End()
/// </summary>
[RequireComponent(typeof(BaseFindTarget))]
public class BaseStartCondition : MonoBehaviour
{
    public enum ConditionType{
        Immediate,
        Delayed,
        Custom
    }

    public ConditionType conditionType = ConditionType.Immediate;
    public Type influence = typeof(BaseInfluence);
    
    protected BaseFindTarget findTarget = null;
    protected List<GameObject> influencedObjects = new List<GameObject>();

    protected virtual void Awake(){
        findTarget = GetComponent<BaseFindTarget>();}

    protected virtual void Start(){}

    protected virtual void Update(){
        if (findTarget==null || !findTarget.isActiveAndEnabled) {
            End();
            return;
        }


        switch(conditionType){
            case ConditionType.Immediate:
                InfluenceFoundGameObjects();
            break;
        
            case ConditionType.Delayed:
                var targets = findTarget.GetTargets();
                foreach(var target in targets){
                if (influencedObjects.Contains(target)) continue;
                    MyTools.Cooldown cd = new MyTools.Cooldown(1, ()=>{
                        _StartFunction(target);
                    });
                    cd.StartCoolDown();
                    influencedObjects.Add(target);
                }
            break;
            case ConditionType.Custom:
                if (CustomCheck()){
                    InfluenceFoundGameObjects();
                }
            break;
        }
    }

    protected virtual bool CustomCheck(){
        return false;
    }



    protected virtual void InfluenceFoundGameObjects(){
        var targets = findTarget.GetTargets();
        foreach (var target in targets){
            _StartFunction(target);
        }
    } 


    protected virtual void End(){
        gameObject.SetActive(false);
        Destroy(gameObject);
    }

    protected void _StartFunction(GameObject obj){
        if (obj==null) return;
        if (influencedObjects.Contains(obj)) return;
        obj.AddComponent(influence);
        influencedObjects.Add(obj);
    }
}
